<template>
  <div class="add-list">
    <div v-if="showList">
      <itemBlock
        v-for="(item, index) in listData"
        :key="'item_' + index"
        :ref="'item_' + index"
        :formData="item"
        :hideBtn="hideBtn"
        :orderStatus="orderStatus"
        :orderType="orderType"
        :planTime="planTime"
        :showOnly="showOnly"
        :pageType="pageType"
        :changeContent="changeContent"
      >
        <template slot="top-botton">
          <span class="index-num">执行信息 {{ index + 1 }}</span>
          <div v-if="!hideBtn" class="title-btn-right">
            <el-tooltip
              class="item"
              effect="dark"
              :disabled="showSaveBtn"
              :content="saveTitle"
              placement="top-start"
            >
              <div class="btnDiv">
                <el-button
                  class="btn-text-blue"
                  size="mini"
                  icon="el-icon-check"
                  :disabled="!showSaveBtn"
                  @click="handleSave(index)"
                  >保存</el-button
                >
              </div>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              :disabled="showDeleteBtn"
              :content="deleteTitle"
              placement="top-start"
            >
              <div class="btnDiv">
                <el-button
                  class="btn-text-blue"
                  size="mini"
                  icon="el-icon-delete"
                  :disabled="!showDeleteBtn"
                  @click="handleDelete(item, index)"
                  >删除</el-button
                >
              </div>
            </el-tooltip>
          </div>
          <div v-if="changeContent" class="title-btn-right">
            <div class="btnDiv">
              <el-button
                class="btn-text-blue"
                size="mini"
                icon="el-icon-check"
                @click="handleSave(index)"
                >保存</el-button
              >
            </div>
          </div>
        </template>
      </itemBlock>
    </div>
  </div>
</template>

<script>
import itemBlock from "./itemBlock";
export default {
  components: {
    itemBlock,
  },
  props: {
    itemList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    btnJurisdiction: {
      type: Array,
      default: () => {
        return [];
      },
    },
    orderStatus: {
      type: String,
      default: "",
    },
    hideBtn: {
      type: Boolean,
      defalut: false,
    },
    orderType: {
      type: String,
      default: "",
    },
    pageType: {
      type: String,
      default: "",
    },
    //仅作为信息展示
    showOnly: {
      type: Boolean,
      default: false,
    },
    planTime: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    itemList: {
      handler(val) {
        this.listData = val;
      },
      deep: true,
    },
  },
  computed: {
    showSaveBtn() {
      if (["defect", "maintain", "repair"].includes(this.orderType)) {
        //,"overhaul"
        return (
          this.$store.getters["isBtnAuth"]("addExecute") &&
          this.$utils.getJurisdiction(this.btnJurisdiction, "addExecute")
        );
      } else {
        return true;
      }
    },
    showDeleteBtn() {
      if (["defect", "maintain", "repair"].includes(this.orderType)) {
        //,"overhaul"
        return (
          this.$store.getters["isBtnAuth"]("deleteExecute") &&
          this.$utils.getJurisdiction(this.btnJurisdiction, "deleteExecute")
        );
      } else {
        return true;
      }
    },
    saveTitle() {
      return this.$utils.getJurisdictionContent(
        this.btnJurisdiction,
        "addExecute",
        this.$store.getters["isBtnAuth"]("addExecute")
      );
    },
    deleteTitle() {
      return this.$utils.getJurisdictionContent(
        this.btnJurisdiction,
        "deleteExecute",
        this.$store.getters["isBtnAuth"]("deleteExecute")
      );
    },
    // 缺陷工单-已归档-缺陷工单管理员岗位 可以修改执行信息的维护内容并保存
    changeContent() {
      let power = false; 
      let user = this.$store.getters.currentInfo.currLeader;
      let appId = this.$store.getters.currentAppInfo.appId;
      let isPosition = this.$utils.isPosition(
        user,
        "缺陷工单管理员",
        appId
      );
      if (this.orderStatus === 'ARCHIVED' && this.orderType === 'defect' && isPosition) {
        power = true;
      }
      return power;
    }
  },
  data() {
    return {
      listData: [],
      showList: true,
    };
  },
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.listData = this.itemList;
    },
    /**移除 */
    async handleDelete(item, num) {
      this.$emit("handleDelete", item, num);
    },
    //获取去掉删除项后的所有填写的执行信息
    async getListData(num) {
      let listArr = [];
      for (let i = 0; i < this.itemList.length; i++) {
        if (i != num) {
          //获取所有填写的执行信息，不进行校验
          let data = await this.$refs["item_" + i][0].getFormData(false);
          listArr.push(data);
        }
      }
      return listArr;
    },
    //保存执行信息
    async handleSave(index) {
      let data = await this.$refs["item_" + index][0].getFormData();
      let orderExecutePart =
        (await this.$refs["item_" + index][0].getPartsData()) || [];
      if (orderExecutePart.length > 0) {
        orderExecutePart.map((i) => {
          delete i.action;
          i.orderType = this.orderType;
        });
      }
      data.orderExecutePart = orderExecutePart;
      this.$emit("handleSave", data, index);
    },
    //获取完工图片附件
    getImageFile() {
      this.itemList.forEach((item, index) => {
        let files = this.$refs["item_" + index][0].getImageFile();
        let uploaded = files.find((i) => {
          if (!Object.hasOwn(i, 'id')) {
            return i;
          }
        });
        if (files.length == 0) {
          throw `执行信息${index + 1}缺少完工图片`;
        } else if (uploaded) {
          throw `执行信息${index + 1}完工图片未保存`;
        }
      });
    },
    //保存执行信息附件
    saveAttach(index, id) {
      this.$refs["item_" + index][0].setAttachType(id);
    },
    deleteAttach(index) {
      this.$refs["item_" + index][0].deleteAttach();
    },
  },
};
</script>

<style lang="less">
.add-list {
  .add-item {
    text-align: center;
    padding: 10px;
  }
  .index-num {
    font-weight: bold;
    font-size: 16px;
  }
  .title-btn-right {
    .el-button {
      // font-size: 20px;
      line-height: 10px;
    }
  }
  .btnDiv {
    display: inline-block;
    margin: 0 10px;
    width: auto !important;
  }
}
</style>
